Imports System.Data
Imports System.Data.SqlClient

Imports TaxonNamesInterface

Public Class UserData

    Private Shared ConnectionString As String = Configuration.ConfigurationManager.ConnectionStrings("names").ConnectionString

    Private Shared Function LoadUserFromDs(ByVal dbuser As User) As TaxonNamesInterface.User
        Dim u As New TaxonNamesInterface.User

        u.Contact = dbuser.Contact
        u.FullName = dbuser.FullName
        u.Login = dbuser.Login
        u.UserId = dbuser.UserId

        Return u
    End Function

    Public Shared Function GetUsers() As List(Of TaxonNamesInterface.User)
        Dim us As New List(Of TaxonNamesInterface.User)
        Dim db As New UserDB(ConnectionString)

        Dim users = (From tbl In db.Users Select tbl)
        For Each u In users
            us.Add(LoadUserFromDs(u))
        Next

        Return us
    End Function

    Public Shared Function Login(ByVal u As TaxonNamesInterface.User, ByVal pwd As String) As Boolean
        Dim ok As Boolean = False

        Dim db As New UserDB(ConnectionString)

        Dim dbpwd = (From tbl In db.Users Where tbl.UserId = u.UserId Select tbl.Password).First
        If dbpwd IsNot Nothing AndAlso dbpwd.Equals(pwd) Then
            ok = True
        End If

        Return ok
    End Function


End Class
